#!/bin/bash

# ============================================
# 修复 Nginx 后端代理配置
# ============================================

NGINX_CONF="/etc/nginx/conf.d/frontend.conf"

echo "=== 修复后端代理配置 ==="
echo ""

# 检查当前配置
echo "当前 proxy_pass 配置:"
grep "proxy_pass" "$NGINX_CONF" | grep -v "#"

# 更新为本地地址（更安全，因为 Nginx 和后端在同一服务器）
echo ""
echo "更新 proxy_pass 为 127.0.0.1:8000..."
sudo sed -i 's|proxy_pass http://10.170.129.4:8000/api/;|proxy_pass http://127.0.0.1:8000/api/;|' "$NGINX_CONF"

echo "✓ 配置已更新"
echo ""
echo "更新后的配置:"
grep "proxy_pass" "$NGINX_CONF" | grep -v "#"

# 验证配置
echo ""
echo "验证配置..."
if sudo nginx -t; then
    echo "✓ 配置测试通过"
else
    echo "✗ 配置测试失败"
    exit 1
fi

# 重新加载
echo ""
echo "重新加载 Nginx..."
sudo systemctl reload nginx

echo ""
echo "✓ 完成！"
echo ""
echo "现在前端请求 /api/ 会通过 Nginx 代理到后端的 127.0.0.1:8000"
echo "确保 Django 后端正在运行："
echo "  python manage.py runserver 0.0.0.0:8000"
echo "  或者"
echo "  python manage.py runserver 127.0.0.1:8000"

